<template>
  <div style="display: flex">
    <div
      :class="{ active: index === active }"
      class="tabs"
      v-for="(item, index) in data"
      :key="item.name"
      @click="switchCom(index, item)"
    >
      <div>{{ item.name }}</div>
    </div>
  </div>

  <component :is="comId"></component>
</template>

<script setup lang="ts">
import { ref, shallowRef } from "vue";
import A from "./A/index.vue";
import B from "./B/index.vue";
import C from "./C/index.vue";

const active = ref(0);
const comId = shallowRef(A);
const data = shallowRef([
  {
    name: "A组件",
    com: A,
  },
  {
    name: "B组件",
    com: B,
  },
  {
    name: "C组件",
    com: C,
  },
]);

const switchCom = (index: number, item: any) => {
  active.value = index;
  comId.value = item.com;
};
</script>

<style scoped>
.tabs {
  border: 1px solid #ccc;
  padding: 5px 10px;
  margin: 5px;
  cursor: pointer;
}

.active {
  background: #ccc;
}
</style>
